#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x) : val(x), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
    {
    }
};

class Solution
{
private:
    vector<int> vec;
    void recursion(TreeNode *node)
    {
        if (node == nullptr)
        {
            return;
        }
        recursion(node->left);
        vec.push_back(node->val);
        recursion(node->right);
    }
public:
    int getMinimumDifference(TreeNode *root)
    {
        vec.clear();
        recursion(root);
        if (vec.size() < 2)
        {
            return 0;
        }
        int result = INT_MAX;
        for (int i = 1; i < vec.size(); i++)
        {
            result = min(result, vec[i] - vec[i - 1]);
        }
        return result;
    }
};

// 辅助函数：从数组构建二叉搜索树（层序遍历方式，-1表示null）
TreeNode *buildTreeFromArray(vector<int> &arr)
{
    if (arr.empty() || arr[0] == -1) return nullptr;

    TreeNode *root = new TreeNode(arr[0]);
    queue<TreeNode *> q;
    q.push(root);

    int i = 1;
    while (!q.empty() && i < arr.size())
    {
        TreeNode *node = q.front();
        q.pop();

        // 左子节点
        if (i < arr.size() && arr[i] != -1)
        {
            node->left = new TreeNode(arr[i]);
            q.push(node->left);
        }
        i++;

        // 右子节点
        if (i < arr.size() && arr[i] != -1)
        {
            node->right = new TreeNode(arr[i]);
            q.push(node->right);
        }
        i++;
    }

    return root;
}

// 辅助函数：层序遍历打印二叉树
void printTree(TreeNode *root)
{
    if (!root)
    {
        cout << "[]" << endl;
        return;
    }

    queue<TreeNode *> q;
    q.push(root);
    vector<string> result;

    while (!q.empty())
    {
        TreeNode *node = q.front();
        q.pop();

        if (node)
        {
            result.push_back(to_string(node->val));
            q.push(node->left);
            q.push(node->right);
        } else
        {
            result.push_back("null");
        }
    }

    // 移除末尾的null
    while (!result.empty() && result.back() == "null")
    {
        result.pop_back();
    }

    cout << "[";
    for (int i = 0; i < result.size(); i++)
    {
        cout << result[i];
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode *root)
{
    if (!root) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main()
{
    Solution solution;

    cout << "=== LeetCode 530. 二叉搜索树的最小绝对差 测试案例 ===" << endl << endl;

    // 测试案例1：题目示例1
    cout << "测试案例1：" << endl;
    vector<int> tree1 = {4, 2, 6, 1, 3};
    TreeNode *root1 = buildTreeFromArray(tree1);

    cout << "输入: root = ";
    printTree(root1);

    int result1 = solution.getMinimumDifference(root1);
    cout << "输出: " << result1 << endl;
    cout << "期望: 1" << endl << endl;

    // 测试案例2：题目示例2
    cout << "测试案例2：" << endl;
    vector<int> tree2 = {1, 0, 48, -1, -1, 12, 49};
    TreeNode *root2 = buildTreeFromArray(tree2);

    cout << "输入: root = ";
    printTree(root2);

    int result2 = solution.getMinimumDifference(root2);
    cout << "输出: " << result2 << endl;
    cout << "期望: 1" << endl << endl;

    // 测试案例3：单节点树
    cout << "测试案例3（单节点树）：" << endl;
    vector<int> tree3 = {1};
    TreeNode *root3 = buildTreeFromArray(tree3);

    cout << "输入: root = ";
    printTree(root3);

    int result3 = solution.getMinimumDifference(root3);
    cout << "输出: " << result3 << endl;
    cout << "期望: 0" << endl << endl;

    // 测试案例4：两个节点的树
    cout << "测试案例4（两个节点）：" << endl;
    vector<int> tree4 = {1, -1, 3};
    TreeNode *root4 = buildTreeFromArray(tree4);

    cout << "输入: root = ";
    printTree(root4);

    int result4 = solution.getMinimumDifference(root4);
    cout << "输出: " << result4 << endl;
    cout << "期望: 2" << endl << endl;

    // 测试案例5：连续数字的BST
    cout << "测试案例5（连续数字）：" << endl;
    vector<int> tree5 = {5, 4, 6, 3, -1, -1, 7};
    TreeNode *root5 = buildTreeFromArray(tree5);

    cout << "输入: root = ";
    printTree(root5);

    int result5 = solution.getMinimumDifference(root5);
    cout << "输出: " << result5 << endl;
    cout << "期望: 1" << endl << endl;

    // 测试案例6：较大差值的BST
    cout << "测试案例6（较大差值）：" << endl;
    vector<int> tree6 = {10, 5, 15, 3, 7, 13, 18};
    TreeNode *root6 = buildTreeFromArray(tree6);

    cout << "输入: root = ";
    printTree(root6);

    int result6 = solution.getMinimumDifference(root6);
    cout << "输出: " << result6 << endl;
    cout << "期望: 2" << endl << endl;

    // 测试案例7：只有左子树
    cout << "测试案例7（只有左子树）：" << endl;
    vector<int> tree7 = {3, 2, -1, 1};
    TreeNode *root7 = buildTreeFromArray(tree7);

    cout << "输入: root = ";
    printTree(root7);

    int result7 = solution.getMinimumDifference(root7);
    cout << "输出: " << result7 << endl;
    cout << "期望: 1" << endl << endl;

    // 清理内存
    deleteTree(root1);
    deleteTree(root2);
    deleteTree(root3);
    deleteTree(root4);
    deleteTree(root5);
    deleteTree(root6);
    deleteTree(root7);

    cout << "所有测试案例执行完毕！" << endl;

    return 0;
}
